﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cube
{
    public partial class LoginFrm : Form
    {
        private String m_User;
        private int m_UserLevel;
        private List<String[]> m_AlreadyCreatedUsers;
        private bool m_Admin = false;
        /// <summary>
        /// Konstruktor der Login-Form
        /// </summary>
        public LoginFrm()
        {
            InitializeComponent();
            //Bereits bestehende User auslesen
            m_AlreadyCreatedUsers = registryUserData.getAllUsers();
            //Check ob ein Admin-Konto besteht
            for (int i = 0; i < m_AlreadyCreatedUsers.Count; i++)
            {
                if (m_AlreadyCreatedUsers[i][1] == "1")
                    m_Admin = true;
                comboBox_user.Items.Add(m_AlreadyCreatedUsers[i][0]);
                comboBox_user.Text = m_AlreadyCreatedUsers[0][0];
            }
            //Wenn kein User mit Admin-Rechten vorhanden ist ein Admin erstellen,
            //sonst ist es nicht möglich neue User für das Programm anzulegen
            if (m_Admin == false)
            {
                createAdminUserAccount();
                m_User = "ADMIN";
                m_UserLevel = 1;
                comboBox_user.Text = "ADMIN";
                textBox_Password.Text = "admin";
            }
        }
        public String User
        {
            get
            {
                return m_User;
            }
        }
        public int UserLevel
        {
            get
            {
                return m_UserLevel;
            }
        }
        /// <summary>
        /// Erstellt einen Admin-Account in der registry. Wird ausgeführt falls beim Programmstart
        /// kein Account mit Adminrechten existiert
        /// </summary>
        private void createAdminUserAccount()
        {
            registryUserData.insertUserData("ADMIN", md5Encrypter.CreateMD5("admin"), "1");
        }

        private void button_Login_Click(object sender, EventArgs e)
        {
            if (comboBox_user.Text != "" && textBox_Password.Text != "")
            {
                if (checkUserData(comboBox_user.Text, textBox_Password.Text))
                {
                    statusStrip1.BackColor = Color.Green;
                    StatusLabel.Text = Properties.Resources.UserString + " " + m_User + " " + Properties.Resources.LoggingInString;
                    Application.DoEvents();
                    m_User = comboBox_user.Text;
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    statusStrip1.BackColor = Color.Red;
                    StatusLabel.Text = Properties.Resources.LoginincorrectString;
                }
            }
        }
        /// <summary>
        /// Prüft ob der angegebene User in der registry existiert und das Passwort korrekt ist
        /// </summary>
        /// <param name="user"></param>
        /// <param name="pw"></param>
        /// <returns></returns>
        private bool checkUserData(String user, String pw)
        {
            String loginPW;
            String[] userData;
            try
            {
                userData = registryUserData.getUserData(user);
                if ((userData[0] != "") && (userData[1] != ""))
                {
                    loginPW = md5Encrypter.CreateMD5(pw);
                    if (userData[0] == loginPW)
                    {
                        m_UserLevel = Convert.ToInt32(userData[1]);
                        return true;
                    }
                    else
                        return false;
                }
                else
                    return false;
            }
            catch { return false; }
        }

        private void button_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void textBox_Password_KeyDown(object sender, KeyEventArgs e)
        {
            //Wenn enter gedrückt wurde wird der Login gestartet
            if (e.KeyValue == 13)
            {
                button_Login_Click(this, e);
            }
        }

        private void LoginFrm_Shown(object sender, EventArgs e)
        {
            if(m_Admin==false)
                button_Login_Click(this, new EventArgs());
        }

        private void textBox_Password_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == '\r')
                e.Handled = true;
        }
    }
}
